%% computes the shift in the distributions after the policy shock   
%% south ip2=3,ip=1
dR         = log(A_vec_s(ip,ip2))-log(A_vec_s(1,1)) +log(b_bar_s)-log(b_bar_ss);
n_stnu    = ceil(abs(dR)/db_mat);%max stnu down
if  dR>0.001 && shiftdist==1
    weight_step    = 1-(n_stnu-dR/db_mat);%weight on max step down
    f_dist0_mat    = reshape(f_dist_ss_s,N_z,N_b);
    f_dist0_mat_new= zeros(N_z,N_b);
    f_dist0_mat_new(:,1:end-n_stnu+1)=(1-weight_step)*f_dist0_mat(:,n_stnu:end);
    f_dist0_mat_new(:,1:end-n_stnu)  =f_dist0_mat_new(:,1:end-n_stnu)+weight_step*f_dist0_mat(:,n_stnu+1:end);
    f_dist0_mat_new(:,1)              =f_dist0_mat_new(:,1)+sum(f_dist0_mat(:,1:n_stnu-1),2)+weight_step*f_dist0_mat(:,n_stnu);
    f_dist0_s                         =reshape(f_dist0_mat_new,N_z*N_b,1);
    f_dist0_s(inde_bb_s)=0;
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_s(ip,ip2)=0;
    end

elseif  dR<-0.001 && shiftdist==1
    weight_step    = 1-(n_stnu-abs(dR)/db_mat);%weight on max step down
    %initial distribution
    f_dist0_mat    = reshape(f_dist_ss_s,N_z,N_b);
    %distribution after shift in value added, before renegotiation
    f_dist0_mat_new= zeros(N_z,N_b);
    f_dist0_mat_new(:,n_stnu+1:end)  =(1-weight_step)*f_dist0_mat(:,1:end-n_stnu);
    f_dist0_mat_new(:,n_stnu:end)    =f_dist0_mat_new(:,n_stnu:end)+weight_step*f_dist0_mat(:,1:end-n_stnu+1);

    %compute disribution of firms upon renegotiation
    h_dist_nego0_s_new=zeros(N_z,N_b);
    h_dist_nego0_s_new(:,inde_nego_s:min(N_b,inde_nego_s+n_stnu)-1)  =phi_s*(1-weight_step)*f_dist0_mat(:,end-n_stnu+1+max(0,inde_nego_s+n_stnu-N_b):end);
    h_dist_nego0_s_new(:,inde_nego_s:min(N_b,inde_nego_s+n_stnu))    =h_dist_nego0_s_new(:,inde_nego_s:min(N_b,inde_nego_s+n_stnu))+phi_s*weight_step*f_dist0_mat(:,end-n_stnu+max(0,inde_nego_s+n_stnu-N_b):end);

    %compute disribution after renegotiation
    f_dist0_s                         =reshape(f_dist0_mat_new+h_dist_nego0_s_new,N_z*N_b,1);
    f_dist0_s(inde_bb_s)=0;
    %mass of exiters
    mass_exit_0_s                     =(1-phi_s)*sum(sum(f_dist0_mat(:,end-n_stnu+2:end),2)+(1-weight_step)*f_dist0_mat(:,end-n_stnu+1));
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_s(ip,ip2)              =mass_exit_0_s/sum(sum(f_dist0_mat));
    end

else
    f_dist0_s=f_dist_ss_s;
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_s(ip,ip2)=0;
    end
end

%% north
dR         = log(A_vec_n(ip,ip2))-log(A_vec_n(1,1));
n_stnu      = ceil(abs(dR)/db_mat);%max stnu down
if   dR>0.001 && shiftdist==1
    weight_step  = 1-(n_stnu-dR/db_mat);%weight on max step down
    f_dist0_mat  = reshape(f_dist_ss_n,N_z,N_b);
    f_dist0_mat_new=zeros(N_z,N_b);
    f_dist0_mat_new(:,1:end-n_stnu+1)=(1-weight_step)*f_dist0_mat(:,n_stnu:end);
    f_dist0_mat_new(:,1:end-n_stnu)  =f_dist0_mat_new(:,1:end-n_stnu)+weight_step*f_dist0_mat(:,n_stnu+1:end);
    f_dist0_mat_new(:,1)              =f_dist0_mat_new(:,1)+sum(f_dist0_mat(:,1:n_stnu-1),2)+weight_step*f_dist0_mat(:,n_stnu);
    f_dist0_n                         =reshape(f_dist0_mat_new,N_z*N_b,1);
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_n(ip,ip2)                      =0;
    end

elseif  dR<-0.001 && shiftdist==1
    weight_step  = 1-(n_stnu-abs(dR)/db_mat);%weight on max step down
    f_dist0_mat  = reshape(f_dist_ss_n,N_z,N_b);
    f_dist0_mat_new=zeros(N_z,N_b);
    f_dist0_mat_new(:,n_stnu+1:end)  =(1-weight_step)*f_dist0_mat(:,1:end-n_stnu);
    f_dist0_mat_new(:,n_stnu:end)    =f_dist0_mat_new(:,n_stnu:end)+weight_step*f_dist0_mat(:,1:end-n_stnu+1);

    %compute disribution of firms upon renegotiation
    h_dist_nego0_n_new=zeros(N_z,N_b);
    h_dist_nego0_n_new(:,inde_nego_n:inde_nego_n+n_stnu-1)  =phi_n*(1-weight_step)*f_dist0_mat(:,end-n_stnu+1:end);
    h_dist_nego0_n_new(:,inde_nego_n:inde_nego_n+n_stnu)    =h_dist_nego0_n_new(:,inde_nego_n:inde_nego_n+n_stnu)+phi_n*weight_step*f_dist0_mat(:,end-n_stnu:end);
    %compute disribution after renegotiation
    f_dist0_n                                                =reshape(f_dist0_mat_new+h_dist_nego0_n_new,N_z*N_b,1);

    %mass of exiters
    mass_exit_0_n                     =(1-phi)*sum(sum(f_dist0_mat(:,end-n_stnu+2:end),2)+(1-weight_step)*f_dist0_mat(:,end-n_stnu+1));
    
    
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_n(ip,ip2)                      =mass_exit_0_n/sum(sum(f_dist0_mat));
    end

else
    f_dist0_n                         =f_dist_ss_n;
    if  inde_dilution  ==1 && l_reallocation==1 &&  demand_ext==1 && shiftdist==1
        exit_0_n(ip,ip2)=0;
    end
end



